﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Battle
{
    public class Bullet_Tow : BulletBase
    {
        protected const float ShowTime = 1f;
        protected const float WaitTime = 0.3f;
        protected const float LineWidth = 5;
        protected const string TexturePath = "PowerGridTexture";

        /// <summary>
        /// 用来记录正在别牵引的对象，避免重复生效
        /// </summary>
        protected static List<ICanHitObj> listTowObj=new List<ICanHitObj>();
        protected static Texture lineTexture;

        protected ICanHitObj targetObj;
        protected ICanHitObj[] enemys;
        protected Vector3 Center => (targetObj != null&&!targetObj.IsDeath) ? targetObj.Pos : transform.position;

        protected float showStartTime;

        protected override void OnHitObj(ICanHitObj target)
        {
            if (showStartTime == 0)
                base.OnHitObj(target);
        }

        protected override void AfterHitObj(ICanHitObj target)
        {
            //取消弹射，穿透,移动
            moveComponent.enabled = false;

            if (!(target is Player.Fort.FortWall))
                targetObj = target;
            //牵引
            TowAsync();
        }

        protected async void TowAsync()
        {
            enemys = EnemyMgr.Ins.GetEnemysInRange(Center, (float)BulletData.rangeData.dRadius);

            var hitInfo = new ST_HitActionData()
            {
                nRepelNum=BulletData.nRepelNum,
            };

            if (lineTexture == null)
                lineTexture = await UnityEngine.AddressableAssets.Addressables.LoadAssetAsync<Texture>(TexturePath).Task;

            int needNum = (targetObj != null && !targetObj.IsDeath) ? 2 : 1;
            if (enemys == null || enemys.Length <needNum )
                DestorySelf();
            else
            {
                showStartTime = Time.time;

                await System.Threading.Tasks.Task.Delay((int)(WaitTime*1000));

                //周围加击退力
                for (int i = 0; i < enemys.Length; i++)
                {
                    if (enemys[i] != null && !enemys[i].IsDeath && enemys[i] != targetObj)
                    {
                        if (listTowObj.Contains(enemys[i])) continue;
                        else listTowObj.Add(enemys[i]);

                        hitInfo.vRepelDir = (Center - enemys[i].Pos).normalized;
                        enemys[i].OnHit(hitInfo);
                    }
                }
            }
        }

        public override void DestorySelf()
        {
            base.DestorySelf();

            if(enemys!=null)
                for (int i = 0; i < enemys.Length; i++)
                {
                    listTowObj.Remove(enemys[i]);
                }
        }

        private void OnGUI()
        {
            if (showStartTime == 0) return;

            for (int i = 0; i < enemys.Length; i++)
            {
                if (enemys[i] != null && !enemys[i].IsDeath)
                    SangsTools.GUI_Tools.GUI_DrawLine_World(Center, enemys[i].Pos, LineWidth, lineTexture);
            }

            if (Time.time - showStartTime >= ShowTime)
            {
                DestorySelf();
                showStartTime = 0;
            }
        }
    }
}
